﻿using CCWin;
using CCWin.SkinControl;
using HirelSeriesH.Entity;
using HirelSeriesH.Global;
using HirelSeriesH.Tools;
using HML;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.PeerToPeer.Collaboration;
using System.Reflection;
using System.Runtime.Remoting.Channels;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using static HirelSeriesH.Entity.Power;

namespace HirelSeriesH
{
    public partial class HomeForm : UIForm
    {

        //主窗口不显示左上角关闭按钮。
        private const int NOCLOSE_BUTT0N = 0x200;

        public System.Timers.Timer TimerForBox = new System.Timers.Timer();

        public System.Timers.Timer TimerForPower = new System.Timers.Timer();

        public System.Timers.Timer TimerForRushChannel = new System.Timers.Timer();

        List<RoundContainer> TitleContainerChannelList = new List<RoundContainer>();
        List<RoundContainer> ChannelPanelList = new List<RoundContainer>();

        List<SkinLabel> ChannelStateList = new List<SkinLabel>();
        List<SkinLabel> ChannelCPXHList = new List<SkinLabel>();
        List<SkinLabel> ChannelCPPHList = new List<SkinLabel>();

        List<SkinLabel> ChannelOutVoltList = new List<SkinLabel>();

        Channel ChannelCurrent = null;


        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams createParams = base.CreateParams;
                //主窗口不显示左上角关闭按钮。
                createParams.ClassStyle = NOCLOSE_BUTT0N;
                return createParams;
            }
        }

        public HomeForm()
        {
            InitializeComponent();
            //使线程可以调用窗体控件
            CheckForIllegalCrossThreadCalls = false;

            ChannelPanel01.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel01.Click += new EventHandler(this.ChannelPanel_Click);
            state1.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh1.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH1.Click += new EventHandler(this.ChannelPanel_Click);
            C1Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C1Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C1Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C1Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel02.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel02.Click += new EventHandler(this.ChannelPanel_Click);
            state2.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh2.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH2.Click += new EventHandler(this.ChannelPanel_Click);
            C2Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C2Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C2Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C2Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel03.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel03.Click += new EventHandler(this.ChannelPanel_Click);
            state3.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh3.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH3.Click += new EventHandler(this.ChannelPanel_Click);
            C3Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C3Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C3Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C3Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel04.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel04.Click += new EventHandler(this.ChannelPanel_Click);
            state4.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh4.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH4.Click += new EventHandler(this.ChannelPanel_Click);
            C4Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C4Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C4Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C4Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel05.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel05.Click += new EventHandler(this.ChannelPanel_Click);
            state5.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh5.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH5.Click += new EventHandler(this.ChannelPanel_Click);
            C5Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C5Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C5Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C5Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel06.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel06.Click += new EventHandler(this.ChannelPanel_Click);
            state6.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh6.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH6.Click += new EventHandler(this.ChannelPanel_Click);
            C6Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C6Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C6Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C6Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel07.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel07.Click += new EventHandler(this.ChannelPanel_Click);
            state7.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh7.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH7.Click += new EventHandler(this.ChannelPanel_Click);
            C7Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C7Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C7Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C7Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            ChannelPanel08.Click += new EventHandler(this.ChannelPanel_Click);
            TitlePanel08.Click += new EventHandler(this.ChannelPanel_Click);
            state8.Click += new EventHandler(this.ChannelPanel_Click);
            cpxh8.Click += new EventHandler(this.ChannelPanel_Click);
            CPPH8.Click += new EventHandler(this.ChannelPanel_Click);
            C8Pow1.Click += new EventHandler(this.ChannelPanel_Click);
            C8Pow2.Click += new EventHandler(this.ChannelPanel_Click);
            C8Pow3.Click += new EventHandler(this.ChannelPanel_Click);
            C8Pow4.Click += new EventHandler(this.ChannelPanel_Click);

            TitleContainerChannelList.Add(TitlePanel01);
            TitleContainerChannelList.Add(TitlePanel02);
            TitleContainerChannelList.Add(TitlePanel03);
            TitleContainerChannelList.Add(TitlePanel04);
            TitleContainerChannelList.Add(TitlePanel05);
            TitleContainerChannelList.Add(TitlePanel06);
            TitleContainerChannelList.Add(TitlePanel07);
            TitleContainerChannelList.Add(TitlePanel08);

            ChannelPanelList.Add(ChannelPanel01);
            ChannelPanelList.Add(ChannelPanel02);
            ChannelPanelList.Add(ChannelPanel03);
            ChannelPanelList.Add(ChannelPanel04);
            ChannelPanelList.Add(ChannelPanel05);
            ChannelPanelList.Add(ChannelPanel06);
            ChannelPanelList.Add(ChannelPanel07);
            ChannelPanelList.Add(ChannelPanel08);

            ChannelStateList.Add(state1);
            ChannelStateList.Add(state2);
            ChannelStateList.Add(state3);
            ChannelStateList.Add(state4);
            ChannelStateList.Add(state5);
            ChannelStateList.Add(state6);
            ChannelStateList.Add(state7);
            ChannelStateList.Add(state8);

            ChannelCPXHList.Add(cpxh1);
            ChannelCPXHList.Add(cpxh2);
            ChannelCPXHList.Add(cpxh3);
            ChannelCPXHList.Add(cpxh4);
            ChannelCPXHList.Add(cpxh5);
            ChannelCPXHList.Add(cpxh6);
            ChannelCPXHList.Add(cpxh7);
            ChannelCPXHList.Add(cpxh8);

            ChannelCPPHList.Add(CPPH1);
            ChannelCPPHList.Add(CPPH2);
            ChannelCPPHList.Add(CPPH3);
            ChannelCPPHList.Add(CPPH4);
            ChannelCPPHList.Add(CPPH5);
            ChannelCPPHList.Add(CPPH6);
            ChannelCPPHList.Add(CPPH7);
            ChannelCPPHList.Add(CPPH8);

            ChannelOutVoltList.Add(C1Pow1);
            ChannelOutVoltList.Add(C1Pow2);
            ChannelOutVoltList.Add(C1Pow3);
            ChannelOutVoltList.Add(C1Pow4);

            ChannelOutVoltList.Add(C2Pow1);
            ChannelOutVoltList.Add(C2Pow2);
            ChannelOutVoltList.Add(C2Pow3);
            ChannelOutVoltList.Add(C2Pow4);

            ChannelOutVoltList.Add(C3Pow1);
            ChannelOutVoltList.Add(C3Pow2);
            ChannelOutVoltList.Add(C3Pow3);
            ChannelOutVoltList.Add(C3Pow4);

            ChannelOutVoltList.Add(C4Pow1);
            ChannelOutVoltList.Add(C4Pow2);
            ChannelOutVoltList.Add(C4Pow3);
            ChannelOutVoltList.Add(C4Pow4);

            ChannelOutVoltList.Add(C5Pow1);
            ChannelOutVoltList.Add(C5Pow2);
            ChannelOutVoltList.Add(C5Pow3);
            ChannelOutVoltList.Add(C5Pow4);

            ChannelOutVoltList.Add(C6Pow1);
            ChannelOutVoltList.Add(C6Pow2);
            ChannelOutVoltList.Add(C6Pow3);
            ChannelOutVoltList.Add(C6Pow4);

            ChannelOutVoltList.Add(C7Pow1);
            ChannelOutVoltList.Add(C7Pow2);
            ChannelOutVoltList.Add(C7Pow3);
            ChannelOutVoltList.Add(C7Pow4);

            ChannelOutVoltList.Add(C8Pow1);
            ChannelOutVoltList.Add(C8Pow2);
            ChannelOutVoltList.Add(C8Pow3);
            ChannelOutVoltList.Add(C8Pow4);

            UserCheckForm.refreshParams += refreshParams;

            Thread threadAutoRun = new Thread(new ThreadStart(AutoRun.start));
            threadAutoRun.Start();

            foreach (var channel in Global.Global.ChannelList)
            {
                channel.TimerForTestData.Start();
                channel.TimerForWaveData.Start();
            }

            Global.Global.HomeFrom = this;
            Thread.Sleep(2000);

            WelcomeHelper.CloseForm();
        }

        private void HomeForm_Load(object sender, EventArgs e)
        {
            TimerForBox.Interval = 1000;
            TimerForBox.AutoReset = true;
            TimerForBox.Enabled = false;
            TimerForBox.Elapsed += new ElapsedEventHandler(timerForBox_Tick);
            TimerForBox.Start();

            TimerForPower.Interval = 1000;
            TimerForPower.AutoReset = true;
            TimerForPower.Enabled = false;
            TimerForPower.Elapsed += new ElapsedEventHandler(timerForPower_Tick);
            TimerForPower.Start();

            TimerForRushChannel.Interval = 1000;
            TimerForRushChannel.AutoReset = true;
            TimerForRushChannel.Enabled = false;
            TimerForRushChannel.Elapsed += new ElapsedEventHandler(timerForRushChannel_Tick);
            TimerForRushChannel.Start();
        }

        private void timerForBox_Tick(object sender, EventArgs e)
        {
            try
            {
                PtcBox ptcBox = Global.Global.PtcBox;
                BoxState.Text = "温箱状态：" + Global.Global.CurrenBoxStutas;
                if (Global.Global.PtcBox.ErrorCode != "NULL")
                {
                    BoxState.ForeColor = Color.Red;
                }
                else
                {
                    BoxState.ForeColor = Color.White;
                }
                SetHigh.Text = "设定高温：" + ptcBox.HighTemp.ToString();
                SetLow.Text = "设定低温：" + ptcBox.LowTemp.ToString();
                SetCycles.Text = "温箱设定周期：" + ptcBox.TestCycs.ToString();
                Cycles.Text = "温箱剩余周期：" + Global.Global.Syzq.ToString();
                Temperature.Text = "当前温度：" + Global.Global.CurrenTemperatureStr.ToString();

                /*if (!Global.Global.IsShow && Global.Global.PtcBox.ErrorCode != "NULL")
                {
                    Global.Global.IsShow = true;
                    Thread thread = new Thread(new ThreadStart(showError));
                    thread.Start();
                }*/
            }
            catch (Exception ex)
            {
                string str;
                string strDateInfo = DateTime.Now.ToString() + " 出现应用程序未处理的异常：" + "\r\n";

                if (ex != null)
                {
                    str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                         ex.GetType().Name, ex.Message, ex.StackTrace);
                }
                else
                {
                    str = string.Format("应用程序线程错误:{0}", ex);
                }
                Program.WriteLog(str);
            }
        }

        private void timerForPower_Tick(object sender, EventArgs e)
        {
            try
            {
                if (ChannelCurrent == null)
                {
                    return;
                }
                // 20.00V  OFF  1000分钟  600.00V  1.00A(后续如修改为多组电源，则需要修改逻辑，修改为当前选定的电源组)
                List<Power> PowerList = ChannelCurrent.PowerList;
                for (int i = 0; i < PowerList.Count; i++)
                {
                    Power power = PowerList[i];
                    //string str = power.Voltage + "V(设定值)  ";
                    string str = "";
                    if (power.IsOpen)
                    {
                        /*str = str + power.State.ToUpper() + "  ";

                        if (power.State == "on")
                        {
                            str = str + ((power.OnTimeSec - power.OnTimeWorkedSec) / 60.0m).ToString("0.0") + "分钟  ";
                        }
                        else if (power.State == "off")
                        {
                            str = str + ((power.OffTimeSec - power.OffTimeWorkedSec) / 60.0m).ToString("0.0") + "分钟  ";
                        }
                        else if (power.State == "up")
                        {
                            str = str + "升压中  ";
                        }
                        else if (power.State == "down")
                        {
                            str = str + "降压中  ";
                        }*/

                        /*if (power.State == "on")
                        {
                            str = str + "加电中  ";
                        }
                        else if (power.State == "off")
                        {
                            str = str + "间歇中  ";
                        }
                        else if (power.State == "up")
                        {
                            str = str + "升压中  ";
                        }
                        else if (power.State == "down")
                        {
                            str = str + "降压中  ";
                        }*/

                        str = str + power.RealVoltage + "V  " + power.RealCurrent + "A";
                    }

                    switch (i)
                    {
                        case 0:
                            POW1.Text = "电源1：" + str;
                            break;
                        case 1:
                            POW2.Text = "电源2：" + str;
                            break;
                        case 2:
                            POW3.Text = "电源3：" + str;
                            break;
                        case 3:
                            POW4.Text = "电源4：" + str;
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                string str;
                string strDateInfo = DateTime.Now.ToString() + " 出现应用程序未处理的异常：" + "\r\n";

                if (ex != null)
                {
                    str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                         ex.GetType().Name, ex.Message, ex.StackTrace);
                }
                else
                {
                    str = string.Format("应用程序线程错误:{0}", ex);
                }
                Program.WriteLog(str);
            }
        }

        private void timerForRushChannel_Tick(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < Global.Global.ChannelList.Count; i++)
                {
                    Channel channel = Global.Global.ChannelList[i];
                    if (channel.TestParameter != null)
                    {
                        ChannelCPXHList[i].Text = "产品型号：" + channel.TestParameter.ProductModel;
                        ChannelCPPHList[i].Text = "产品批号：" + channel.TestParameter.BatchNumber;

                        /*List<bool> plist = channel.TestParameter.PowerUseList;
                        for (int j = 0; j < plist.Count; j++)
                        {
                            if (plist[j])
                            {
                                ChannelOutVoltList[i * 4 + j].ForeColor = Color.White;
                                ChannelOutVoltList[i * 4 + j].Text = "电源电压" + (j + 1).ToString() + "：" + channel.PowerList[j].RealVoltage + "V";

                            }
                            else
                            {
                                ChannelOutVoltList[i * 4 + j].ForeColor = Color.FromArgb(24, 24, 24);
                            }
                        }*/
                        for (int j = 0; j < channel.PowerList.Count; j++)
                        {
                            ChannelOutVoltList[i * 4 + j].ForeColor = Color.White;
                            ChannelOutVoltList[i * 4 + j].Text = "DPS" + (j + 1).ToString() + "：" + channel.PowerList[j].RealVoltage + "V";
                        }
                    }
                    else
                    {
                        ChannelCPXHList[i].Text = "产品型号：";
                        ChannelCPPHList[i].Text = "产品批号：";
                        ChannelOutVoltList[i * 4].ForeColor = Color.FromArgb(24, 24, 24);
                        ChannelOutVoltList[i * 4 + 1].ForeColor = Color.FromArgb(24, 24, 24);
                        ChannelOutVoltList[i * 4 + 2].ForeColor = Color.FromArgb(24, 24, 24);
                        ChannelOutVoltList[i * 4 + 3].ForeColor = Color.FromArgb(24, 24, 24);
                    }
                    if (channel.IsRed)
                    {
                        TitleContainerChannelList[i].ContentColor = Color.FromArgb(255, 1, 3);
                        TitleContainerChannelList[i].BorderColor = Color.FromArgb(255, 1, 3);
                    }
                    else
                    {
                        if (channel.State == "stop")
                        {
                            TitleContainerChannelList[i].ContentColor = Color.FromArgb(156, 156, 156);
                            TitleContainerChannelList[i].BorderColor = Color.FromArgb(156, 156, 156);
                        }
                        else if (channel.State == "pause")
                        {
                            TitleContainerChannelList[i].ContentColor = Color.FromArgb(16, 0, 245);
                            TitleContainerChannelList[i].BorderColor = Color.FromArgb(16, 0, 245);
                        }
                        else
                        {
                            TitleContainerChannelList[i].ContentColor = Color.FromArgb(3, 131, 0);
                            TitleContainerChannelList[i].BorderColor = Color.FromArgb(3, 131, 0);
                        }
                    }
                    if (channel.IsCurrentChannel)
                    {
                        TitleContainerChannelList[i].BorderColor = Color.FromArgb(15, 149, 145);
                    }
                    if (channel.State == "stop")
                    {
                        ChannelStateList[i].Text = "运行状态：通道空闲中";
                    }
                    else if (channel.State == "pause")
                    {

                        if (channel.StateEnd)
                        {
                            ChannelStateList[i].Text = "运行状态：测试已完成";
                        }
                        else
                        {
                            ChannelStateList[i].Text = "运行状态：测试暂停中";
                        }
                    }
                    else
                    {
                        ChannelStateList[i].Text = "运行状态：测试进行中";
                    }
                }
            }
            catch (Exception ex)
            {
                string str;
                string strDateInfo = DateTime.Now.ToString() + " 出现应用程序未处理的异常：" + "\r\n";

                if (ex != null)
                {
                    str = string.Format(strDateInfo + "异常类型：{0}\r\n异常消息：{1}\r\n异常信息：{2}\r\n",
                         ex.GetType().Name, ex.Message, ex.StackTrace);
                }
                else
                {
                    str = string.Format("应用程序线程错误:{0}", ex);
                }
                Program.WriteLog(str);
            }
        }

        private void ChannelPanel_Click(object sender, EventArgs e)
        {
            if (Global.Global.ChannelManagementForm == null)
            {
                LoadingForm.LoadingHelper.ShowLoadingScreen();
                ChannelManagementForm channelManagementForm = new ChannelManagementForm(sender, e);
                channelManagementForm.Show();
            }
            else
            {
                Global.Global.ChannelManagementForm.BtnChannel_Click(sender, e);
                Global.Global.ChannelManagementForm.Activate();
            }

            Control button = (Control)sender;
            int channelNum = int.Parse(button.Tag.ToString());
            ChannelCurrent = Global.Global.ChannelList[channelNum];

            for (int i = 0; i < Global.Global.ChannelList.Count; i++)
            {
                Channel channel = Global.Global.ChannelList[i];
                if (channel.IsRed)
                {
                    TitleContainerChannelList[i].BorderColor = Color.FromArgb(255, 1, 3);
                }
                else
                {
                    if (channel.State == "stop")
                    {
                        TitleContainerChannelList[i].BorderColor = Color.FromArgb(156, 156, 156);
                    }
                    else if (channel.State == "pause")
                    {
                        TitleContainerChannelList[i].BorderColor = Color.FromArgb(16, 0, 245);
                    }
                    else
                    {
                        TitleContainerChannelList[i].BorderColor = Color.FromArgb(3, 131, 0);
                    }
                }
                ChannelPanelList[i].BorderColor = Color.FromArgb(24, 24, 24);
            }
            TitleContainerChannelList[channelNum].BorderColor = Color.FromArgb(15, 149, 145);
            ChannelPanelList[channelNum].BorderColor = Color.FromArgb(15, 149, 145);
        }

        //显示系统信息
        private void SysInfoTST_Click(object sender, EventArgs e)
        {
            SysInfoForm sysInfoForm = new SysInfoForm();
            sysInfoForm.ShowDialog(this);
        }

        private void RuleTST_Click(object sender, EventArgs e)
        {
            if (Global.Global.UserRole == "Engineer")
            {
                if (!UIMessageDialog.ShowMessageDialog(this, "当前用户角色为‘工程师’，是否确定切换至‘操作员’角色？", UILocalize.AskTitle, true, UIStyle.Colorful))
                {
                    return;
                }
                else
                {
                    Global.Global.UserRole = "Worker";
                    /*this.TbProductModel.ReadOnly = true;
                    this.TbBatchNumber.ReadOnly = true;
                    this.TbCheckInterval.ReadOnly = true;
                    this.TbDuration.ReadOnly = true;
                    this.TbAlarmCurrent.ReadOnly = true;
                    this.TbRTR.ReadOnly = true;*/

                    if (Global.Global.ChannelManagementForm != null)
                    {
                        Global.Global.ChannelManagementForm.btnTest.Visible = false;
                    }

                    RuleTST.Text = "当前权限：操作员";
                    UIMessageDialog.ShowMessageDialog(this, "当前用户已切换至操作员角色", UILocalize.InfoTitle, false, UIStyle.Colorful);
                }
            }
            else
            {
                UserCheckForm userCheckForm = new UserCheckForm("Engineer", "切换工程师");
                userCheckForm.ShowDialog(this);
            }
        }

        private void refreshParams()
        {
            /*this.TbProductModel.ReadOnly = false;
            this.TbBatchNumber.ReadOnly = false;
            this.TbCheckInterval.ReadOnly = false;
            this.TbDuration.ReadOnly = false;
            this.TbAlarmCurrent.ReadOnly = false;
            this.TbRTR.ReadOnly = false;*/
            if (Global.Global.ChannelManagementForm != null)
            {
                Global.Global.ChannelManagementForm.btnTest.Visible = true;
            }
            RuleTST.Text = "当前权限：工程师";
        }

        //*******************************工具栏***************************//
        private void ChannelManageTSB_Click(object sender, EventArgs e)
        {
            if (Global.Global.ChannelManagementForm == null)
            {
                LoadingForm.LoadingHelper.ShowLoadingScreen();
                ChannelManagementForm channelManagementForm = new ChannelManagementForm(null,null);
                channelManagementForm.Show();
            }
        }

        private void ProgramManageTSB_Click(object sender, EventArgs e)
        {
            if (Global.Global.UserRole == "Engineer")
            {
                ConfigurationForm configurationForm = new ConfigurationForm(false, Global.Global.UserName, "cfg");
                configurationForm.ShowDialog(this);
            }
            else
            {
                UserCheckForm userCheckForm = new UserCheckForm("Engineer", "参数配置");
                userCheckForm.ShowDialog(this);
            }
        }

        private void DataWaveTSB_Click(object sender, EventArgs e)
        {
            Process waveProcess1 = new Process();
            try
            {
                waveProcess1.StartInfo.UseShellExecute = false;
                waveProcess1.StartInfo.WorkingDirectory = "D:\\实时曲线\\实时曲线1#lot";
                waveProcess1.StartInfo.FileName = "D:\\实时曲线\\实时曲线1#lot\\实时曲线.exe";
                waveProcess1.StartInfo.CreateNoWindow = true;
                waveProcess1.Start();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
            }
        }

        private void SysSetTSB_Click(object sender, EventArgs e)
        {
            CMSystem.Show(Cursor.Position);
        }

        //*******************************系统菜单***************************//
        private void TmUser_Click(object sender, EventArgs e)
        {
            UserCheckForm userCheckForm = new UserCheckForm("UserAdmin", "用户管理");
            userCheckForm.ShowDialog(this);
        }

        private void TmPassWord_Click(object sender, EventArgs e)
        {
            ModifyPasswordForm modifyPasswordForm = new ModifyPasswordForm();
            modifyPasswordForm.ShowDialog(this);
        }

        private void TmCorrect_Click(object sender, EventArgs e)
        {
            if (Global.Global.UserRole == "Engineer")
            {
                CorrectForm correctForm = new CorrectForm();
                correctForm.ShowDialog(this);
            }
            else
            {
                UserCheckForm userCheckForm = new UserCheckForm("Engineer", "工厂校准");
                userCheckForm.ShowDialog(this);
            }
        }

        private void TmPowerSettings_Click(object sender, EventArgs e)
        {
            if (Global.Global.UserRole == "Engineer")
            {
                SettingsForm settingsForm = new SettingsForm();
                settingsForm.ShowDialog(this);
            }
            else
            {
                UserCheckForm userCheckForm = new UserCheckForm("Engineer", "出厂设置");
                userCheckForm.ShowDialog(this);
            }
        }

        private void TmPLCReset_Click(object sender, EventArgs e)
        {
            ThreeColor.EequeueCommandQueue("Reset");
        }
        private void HomeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!UIMessageDialog.ShowMessageDialog(this, "关闭系统前请结束各个老化板的测试程序！是否关闭测试系统？", UILocalize.AskTitle, true, UIStyle.Colorful))
            {
                e.Cancel = true;
                return;
            }
            else
            {
                LoadingForm.LoadingHelper.ShowLoadingScreen();
                closeState();
                LoadingForm.LoadingHelper.CloseForm();
                try
                {
                    Environment.Exit(Environment.ExitCode);
                    this.Dispose();
                    this.Close();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.StackTrace);
                }
            }
        }
        private void TmExitSystem_Click(object sender, EventArgs e)
        {
            if (!UIMessageDialog.ShowMessageDialog(this, "关闭系统前请结束各个老化板的测试程序！是否关闭测试系统？", UILocalize.AskTitle, true, UIStyle.Colorful))
            {
                return;
            }
            else
            {
                LoadingForm.LoadingHelper.ShowLoadingScreen();
                closeState();
                LoadingForm.LoadingHelper.CloseForm();
                try
                {
                    Environment.Exit(Environment.ExitCode);
                    this.Dispose();
                    this.Close();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.StackTrace);
                }
            }
        }

        private void closeState()
        {
            AutoRun.stop();

            Thread.Sleep(2000);

            foreach (var channel in Global.Global.ChannelList)
            {
                channel.TimerForTestData.Enabled = false;
                channel.TimerForWaveData.Enabled = false;
            }

            //关闭电源.
            for (int i = 0; i < Global.Global.PowerList.Count; i++)
            {
                if (Global.Global.LowPowerPortList[i] != null && Global.Global.LowPowerPortList[i].IsOpen)
                {
                    //设置初始电压为0
                    Global.Global.LowPowerPortList[i].Write("PV 0" + "\r\n");
                    Thread.Sleep(50);
                    //关闭电源输出
                    Global.Global.LowPowerPortList[i].Write("OUT 0" + "\r\n");
                    Thread.Sleep(50);

                    Global.Global.LowPowerPortList[i].DiscardInBuffer();
                    Global.Global.LowPowerPortList[i].Close();
                }
            }

            if (Global.Global.BoxPort != null && Global.Global.BoxPort.IsOpen)
            {
                //关闭
                string strCommand = "C1=OFF" + "\r\n";
                Global.Global.BoxPort.Write(strCommand);
                //关闭温箱端口
                Global.Global.BoxPort.Close();
            }

            if (Global.Global.PlcPort != null && Global.Global.PlcPort.IsOpen)
            {
                //将指示灯设置为黄色.
                byte[] command = { 0x3A, 0x01, 0x01, 0x03 };
                command = CheckWorker.ADD(command,false);
                Global.Global.PlcPort.Write(command, 0, command.Length);
                Thread.Sleep(300);
                Global.Global.PlcPort.Write(command, 0, command.Length);
                //关闭端口
                Global.Global.PlcPort.Close();
            }

            if (Global.Global.ChannelPort != null && Global.Global.ChannelPort.Opened)
            {
                //老化板停止工作 继电器开合
                //停止运行
                byte[] commandRun = { 0xAA, 0xB0, 0x01, 0x00, 0x00 };
                commandRun = CheckWorker.ADD(commandRun,true);
                Global.Global.ChannelPort.Write(commandRun);
                Thread.Sleep(50);
                //继电器开合
                byte[] mmd = { 0xAA, 0xB0, 0x06, 0x00, 0x00 };
                mmd = CheckWorker.ADD(mmd,true);
                Global.Global.ChannelPort.Write(mmd);

                Global.Global.ChannelPort.Read(Global.Global.ChannelPort.GetNumToRead());
                Global.Global.ChannelPort.Close();
            }

            //threadAutoRun.Abort();
        }

        private void HomeForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.F11)
            {
                RegForm regForm = new RegForm();
                regForm.ShowDialog(this);
            }
        }

        private void TmEM_Click(object sender, EventArgs e)
        {
            EMForm EMForm = new EMForm();
            EMForm.ShowDialog(this);
        }

    }
}
